package com.madixin.chat.exception;

import com.madixin.chat.dto.ResponseResult;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;


/**
 * 统一异常处理
 */
@RestControllerAdvice
public class GlobalExceptionHandlerAdvice {
    Logger logger = LoggerFactory.getLogger(GlobalExceptionHandlerAdvice.class);

    @ExceptionHandler({Exception.class})
    public ResponseResult<String> handlerException(Exception e, HttpServletRequest request, HttpServletResponse response) {
        logger.error("统一异常记录：", e);
        if (e instanceof ChatBaseException) {
            return ResponseResult.fail(((ChatBaseException) e).getErrorCode());
        }
        return ResponseResult.fail(e.getMessage());
    }
}
